﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThirdServices.AlertModel.Providers.Wanderground.Factory;
using ThirdServices.AlertModel.Factory.BaseInterface;
using ThirdServices.AlertModel.Factory.Entities.DTO;
using GNSCore.Repositories;

namespace GNSCore.Managers.Statistics
{
    public class StatisticManager
    {
        private IRepository<Model.GeoData> _geoDataRep;
        private IRepository<Model.GeoExtensionData> _geoExtensionDataRep;


        public StatisticManager(IRepository<Model.GeoData> _geoData,IRepository<Model.GeoExtensionData> _geoExtensionData)
        {
            _geoDataRep = _geoData;
            _geoExtensionDataRep = _geoExtensionData;
        }

        public void SaveStatistic(List<BaseAlert> baseAlertList,string userName)
        {
            if(baseAlertList.Count == 0)
                return;
            
            Console.WriteLine("!!!");
            double avgValue = 0;
            string description = "";
            double differency;

            foreach (BaseAlert baseAlert in baseAlertList)
                avgValue += Convert.ToDouble(baseAlert.NotificationDescription.value);
            avgValue /= baseAlertList.Count;
            DateTime dateAlert = baseAlertList[0].NotificationDescription.date;
            
            if (baseAlertList.Count >= 2)
            {
                differency = 
                    Math.Abs(avgValue - Convert.ToDouble(baseAlertList[0].NotificationDescription.value));
                
                foreach (BaseAlert baseAlert in baseAlertList)
                {
                    if(Math.Abs(avgValue - Convert.ToDouble(baseAlert.NotificationDescription.value)) < differency)
                    {
                        differency = Math.Abs(avgValue - Convert.ToDouble(baseAlert.NotificationDescription.value));
                        dateAlert = baseAlert.NotificationDescription.date;
                        description = baseAlert.NotificationDescription.notification;
                    }
                }
            }

            Model.GeoExtensionData geoExtensionData = new GNSCore.Model.GeoExtensionData();
            geoExtensionData.DataType = (int)baseAlertList[0].NotificationDescription.measure;
            geoExtensionData.Temperature = avgValue;

            Model.GeoData geoData = new GNSCore.Model.GeoData();
            geoData.EventTypeId = (int)baseAlertList[0].NotificationDescription.alert_type;
            geoData.EventDate = dateAlert;
            geoData.Latitiude = 0;
            geoData.Longitude = 0;
            geoData.Description = description;
            geoData.User = null;
            geoData.AdditionalInfoId = null;

            geoData.GeoExtensionData.Add(geoExtensionData);

            _geoDataRep.Add(geoData);
            _geoDataRep.SaveChanges(true);

            Console.WriteLine(avgValue + " " + dateAlert);
        }

        public void SaveStatistic(List<BaseAlert> baseAlertList)
        {
            SaveStatistic(baseAlertList, null);
        }
    }
}
